package com.example.a79273.shandianban;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.shandianban.adapter.ShopCarBaseAdapter;
import com.example.a79273.shandianban.adapter.ShopLikeAdapter;
import com.example.a79273.shandianban.config.NetconfigYouHuo;
import com.example.a79273.shandianban.db.ShopingCar;
import com.example.a79273.shandianban.model.ShopGuessLikeBean;
import com.example.a79273.shandianban.model.YouHuoProductTopImgsBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ShopingCarActivity extends BaseActivity implements View.OnClickListener ,CompoundButton.OnCheckedChangeListener{

    private View mViewTablayout;
    private TextView mTextViewTabTitle;
    private ImageView mImageViewBack;
    private TextView mTextViewMore;
    private ListView mListViewProInfo;
    private CheckBox mCheckBoxAllChoose;
    private TextView mTextViewCountPrice, mTextViewJieSuanCount,mTextViewJieSuan;
    private GridView mGridView;

    //用户的登录信息
    private String mUserName ="tanganna";
    private DbManager mDb;//数据库
    private List<ShopingCar> mData;
    private BaseAdapter mAdapter;
    private float countPrice = 0;
    private List<ShopGuessLikeBean.DataBean.ListsBean> mDataGrid;
    private BaseAdapter mGrideAdapter;
    private RequestParams mParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoping_car);
        initView();
        initData();
        setData();
        setListener();
    }
    private void setListener() {
        mImageViewBack.setOnClickListener(this);
        mTextViewMore.setOnClickListener(this);
        mListViewProInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String proid = mData.get(position).getmProid();
                if(!TextUtils.isEmpty(proid)) {
                    Intent intent = new Intent(ShopingCarActivity.this,ProductInfoActivity.class);
                    intent.putExtra("proid",proid);
                    startActivity(intent);
                }

            }
        });
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mDataGrid != null && mDataGrid.size()>0 && !TextUtils.isEmpty(mDataGrid.get(position).getProid())) {
                    Intent intent = new Intent(ShopingCarActivity.this,ProductInfoActivity.class);
                    intent.putExtra("proid",mDataGrid.get(position).getProid());
                    startActivity(intent);
                }

                
            }
        });
    }

    private void setData() {
        mListViewProInfo.setAdapter(mAdapter);
        mGridView.setAdapter(mGrideAdapter);
    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new ShopCarBaseAdapter(mData,this);
        //-------------------数据库----------------
        DbManager.DaoConfig config = new DbManager.DaoConfig();
        mDb = x.getDb(config);
        selectDB();
        mDataGrid = new ArrayList<>();
        mGrideAdapter = new ShopLikeAdapter(mDataGrid,this);
        //获取GridView里的数据
        mParams = new RequestParams(NetconfigYouHuo.SHOP_GUESS_LIKE);
        x.http().get(mParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(!TextUtils.isEmpty(result)) {
                    Type listType = new TypeToken<LinkedList<ShopGuessLikeBean>>(){}.getType();
                    Gson gson = new Gson();
                    LinkedList<ShopGuessLikeBean> resources = gson.fromJson(result, listType);
                    List<ShopGuessLikeBean.DataBean.ListsBean> lists = resources.get(0).getData().getLists();
                    mDataGrid.addAll(lists);
                    mGrideAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(ShopingCarActivity.this, "网络出小差啦...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initView() {
        mViewTablayout = (View) findViewById(R.id.in_shopingcar_tablaout);
        mTextViewTabTitle = (TextView) mViewTablayout.findViewById(R.id.tv_tablayout_title);
        mTextViewTabTitle.setText("购物车");
        mImageViewBack = (ImageView) mViewTablayout.findViewById(R.id.iv_tablayout_back);
        mImageViewBack.setImageResource(R.mipmap.return_icon);
        mTextViewMore = (TextView) findViewById(R.id.tv_shopingcar_more);
        mListViewProInfo = (ListView) findViewById(R.id.lv_shopingcar_productInfo);
        mGridView = (GridView) findViewById(R.id.rv_shopingcar_tuijian);
        mCheckBoxAllChoose = (CheckBox) findViewById(R.id.cb_shopingcar_chooseall);
        mTextViewCountPrice = (TextView) findViewById(R.id.tv_shopingcar_countprice);
        mTextViewJieSuanCount = (TextView) findViewById(R.id.tv_shopingcar_jisuancount);
        mTextViewJieSuan = (TextView) findViewById(R.id.tv_shopingcar_jisuan);
    }

    //所有的点击事件
    @Override
    public void onClick(View v) {
        Integer position = (Integer) v.getTag();
        switch (v.getId()) {
            case  R.id.iv_tablayout_back:
                finish();
                break;
            case R.id.iv_shopcar_item_add://数量增加
                if(position != null) {
                    ShopingCar shopingCar = mData.get((int)position);
                    shopingCar.setProductCount(""+(Integer.parseInt(shopingCar.getProductCount())+1)); 
                }
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.iv_shopcar_item_sub://数量减少
                if(position != null) {
                    ShopingCar shopingCar = mData.get((int)position);
                    if("1".equals(shopingCar.getProductCount())) {
                        Toast.makeText(ShopingCarActivity.this, "不能再减少了...", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    shopingCar.setProductCount(""+(Integer.parseInt(shopingCar.getProductCount())-1));
                }
                mAdapter.notifyDataSetChanged();
                break;
            case R.id.tv_shopingcar_jisuan://结算去

                break;
            /*case R.id.tv_shopingcar_more:
                startActivity(new Intent(ShopingCarActivity.this,MoreActivity.class));
                break;*/

        }
    }

    //查询数据库里面的数据的方法
    public void selectDB(){
        try {
            List<ShopingCar> shopCarProuducts = mDb.selector(ShopingCar.class).where("username", "=", mUserName).findAll();
            if(shopCarProuducts != null && shopCarProuducts.size()>0) {//购物车里有数据
                mData.addAll(shopCarProuducts);
                mAdapter.notifyDataSetChanged();
            }else{//没数据时

            }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    //CheckBox的点击事件
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case  R.id.cb_shopingcar_chooseall://全选

                break;
            case R.id.cb_shopcar_item_choose:// 每一个商品的选中状态监听
                Integer position = (Integer) buttonView.getTag(R.id.cb_shopcar_item_choose);//获取位置
                float priceTemp = Float.parseFloat((String) buttonView.getTag(R.id.cb_shopingcar_chooseall));//获取价格
                //根据位置获取数量
                int productCount = Integer.parseInt(mData.get(position).getProductCount());
                Log.d("TAG","---"+position+""+priceTemp+""+productCount);
                //当前商品的总价
               float currentPrice = productCount*priceTemp;
                if(buttonView.isChecked()) {
                    countPrice = countPrice+currentPrice;
                }else {
                    countPrice = countPrice-currentPrice;
                }
                mTextViewCountPrice.setText(countPrice+"");
                break;

        }
    }

}
